package com.geek.factory.service.configq;


import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class JobScheduler {


    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    @Autowired
    private org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory;

    public void scheduleProductionPlanCompletionJob(Long planId, Long productId, Date completeTime) throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();

        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("planId", planId);
        jobDataMap.put("productId", productId);
        jobDataMap.put("completeTime", completeTime.toString());
        jobDataMap.put("sqlSessionFactory", sqlSessionFactory);

        JobDetail jobDetail = JobBuilder.newJob(ProductionPlanCompleteJob.class)
                .withIdentity("completeJob_" + planId, "plan_jobs")
                .usingJobData(jobDataMap)
                .build();

        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger_" + planId, "plan_triggers")
                .startAt(completeTime)
                .build();

        scheduler.scheduleJob(jobDetail, trigger);
    }




}
